      SUBROUTINE SAVEINTEGRALS(NOA, NVA, ESCF, EA, TMPMEM, MAXDIM,
     &                   FOCKIJA, FOCKIJABR, FOCKABA, FOCKABABR,
     &                   FOCKIAA, FOCKIAABR,
     &                   FOCKIJAAI, FOCKIJABI, FOCKABAAI,
     &                   FOCKABABI, FOCKIAAAI, FOCKIAABI)
      use mod_iop
      use mod_symm
      use mod_orbit
      use mod_size
C
CWZ      IMPLICIT NONE
      IMPLICIT NONE 
      LOGICAL NONHF
C
      INTEGER, intent(in) :: NOA, NVA, MAXDIM
      DOUBLE PRECISION, intent(in) :: EA(*), ESCF
      DOUBLE PRECISION, intent(in) :: FOCKIJA(NOA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKABA(NVA,NVA)
      DOUBLE PRECISION, intent(in) :: FOCKIAA(NVA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKIJABR(NOA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKABABR(NVA,NVA)
      DOUBLE PRECISION, intent(in) :: FOCKIAABR(NVA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKIJAAI(NOA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKABAAI(NVA,NVA)
      DOUBLE PRECISION, intent(in) :: FOCKIAAAI(NVA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKIJABI(NOA,NOA)
      DOUBLE PRECISION, intent(in) :: FOCKABABI(NVA,NVA)
      DOUBLE PRECISION, intent(in) :: FOCKIAABI(NVA,NOA)
      DOUBLE PRECISION, intent(out) :: TMPMEM(*)
C 
      INTEGER LSTI(20), ISYMOFF(8,8,25)
      INTEGER i,j,k, IL, lst, nsize
      INTEGER NT(2), NF1(2), NF2(2)
      INTEGER IOFFT2(8,8,7)
      INTEGER IOFFAKIJ(8,8,4), IOFFABC(8,8,8)
      INTEGER IOFFIJK(8,8,8)
      INTEGER IOFFABI(8,8,8)
      INTEGER NUMABC(8,8), NUMIJK(8,8), NUMABI(8,8) 
      INTEGER NMAXABC(8), NMAXIJK(8), NMAXABI(8)
      INTEGER IOFFSETW(8,8)
      INTEGER IRREPA(255,2), IINTFP
      INTEGER IAOPOP(8), IRREP, N1, N2
      INTEGER IRPDPD, ISYTYP, NTOT, IRREPI, IRREPJ
      INTEGER I0, I1, I2, NSIZE2
      character*8 fname(19)
      COMMON /SYMPOP/ IRPDPD(8,22),ISYTYP(2,500),NTOT(18)
      INTEGER, external :: IDSYMSZ, IRPDSO, ISYMOFFSO
      INTEGER, external :: IDSYMSOC
      IINTFP=1
      LSTI(1) = 7     !IJKA
      LSTI(2) = 9     !IjAk
      LSTI(3) = 10    !IjKa
      LSTI(4) = 11    !IJKL
      LSTI(5) = 13    !IjKl
      LSTI(6) = 14    !ABIJ
      LSTI(7) = 16    !AbIj
      LSTI(8) = 17    !<Ab|Ij>   as bjAI  
      LSTI(9) = 18    !<Ab|Ij>   as AIbj  
      LSTI(10) = 19   !<AB||IJ>  as AIBJ  
      LSTI(11) = 21   !<Ab|Ij>   as AjbI
      LSTI(12) = 23   !<IA||JB>  as BIAJ
      LSTI(13) = 25   !<iA|jB>   as BiAj
      LSTI(14) = 26   !<aI|bJ>   as bIaJ
      LSTI(15) = 27   !<AB||CI>
      LSTI(16) = 29   !<Ab||Ic>
      LSTI(17) = 30   !<Ab||Ci>
      LSTI(18) = 231  !<AB||CD>
      LSTI(19) = 233  !<Ab||Cd>
      ISYTYP(1,7 )  = 3; ISYTYP(2,7) = 16 
      ISYTYP(1,9 )  = 21;ISYTYP(2,9) = 16
      ISYTYP(1,10) = 21;ISYTYP(2,10) = 16
      ISYTYP(1,11) = 3; ISYTYP(2,11) = 3 
      ISYTYP(1,13) = 21;ISYTYP(2,13) = 21
      ISYTYP(1,14) = 1; ISYTYP(2,14) = 3
      ISYTYP(1,16) = 19;ISYTYP(2,16) = 21
      ISYTYP(1,17) = 9; ISYTYP(2,17) = 9
      ISYTYP(1,18) = 9; ISYTYP(2,18) = 9
      ISYTYP(1,19) = 9; ISYTYP(2,19) = 9
      ISYTYP(1,21) = 9; ISYTYP(2,21) = 9  
      ISYTYP(1,23) = 9; ISYTYP(2,23) = 9
      ISYTYP(1,25) = 9; ISYTYP(2,25) = 9
      ISYTYP(1,26) = 9; ISYTYP(2,26) = 9
      ISYTYP(1,27) = 1; ISYTYP(2,27) = 9
      ISYTYP(1,29) = 19;ISYTYP(2,29) = 16 
      ISYTYP(1,30) = 19;ISYTYP(2,30) = 9
      ISYTYP(1,44) = 1; ISYTYP(2,44) = 3
      ISYTYP(1,46) = 19;ISYTYP(2,46) = 21
      ISYTYP(1,231) = 1;ISYTYP(2,231) = 1
      ISYTYP(1,233) =19;ISYTYP(2,233) =19
      DO IRREP = 1, NIRREP
         IRPDPD(IRREP,1) = IRPDSO(IRREP, VRT, VRT, 0) 
         IRPDPD(IRREP,2) = IRPDSO(IRREP, VRT, VRT, 0) 
         IRPDPD(IRREP,3) = IRPDSO(IRREP, POP, POP, 0)
         IRPDPD(IRREP,4) = IRPDSO(IRREP, POP, POP, 0)
         IRPDPD(IRREP,5) = IRPDSO(IRREP, VRT, VRT, 2)
         IRPDPD(IRREP,6) = IRPDSO(IRREP, VRT, VRT, 2)
         IRPDPD(IRREP,7) = IRPDSO(IRREP, POP, POP, 2)
         IRPDPD(IRREP,8) = IRPDSO(IRREP, POP, POP, 2)
         IRPDPD(IRREP,9) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,10) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,11) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,12) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,13) = IRPDSO(IRREP, VRT, VRT, 1)
         IRPDPD(IRREP,14) = IRPDSO(IRREP, POP, POP, 1)
         IRPDPD(IRREP,15) = IRPDSO(IRREP, VRT, VRT, 1)
         IRPDPD(IRREP,16) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,17) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,18) = IRPDSO(IRREP, VRT, POP, 1)
         IRPDPD(IRREP,19) = IRPDSO(IRREP, VRT, VRT, 1)
         IRPDPD(IRREP,20) = IRPDSO(IRREP, VRT, VRT, 1)
         IRPDPD(IRREP,21) = IRPDSO(IRREP, POP, POP, 1)
         IRPDPD(IRREP,22) = IRPDSO(IRREP, POP, POP, 1)
      ENDDO
      N1 = IDSYMSZ(1,1,1)
      N2 = IDSYMSOC(1, VRTA, VRTA, 0, VRTA, VRTA, 0) 
      WRITE(6,*)'NSIZE OF VVVV', N1, N2 
    
      DO IRREPI = 1, NIRREP
      DO IRREPJ = 1, NIRREP 
         ISYMOFF(IRREPI,IRREPJ,1) = ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,0)
         ISYMOFF(IRREPI,IRREPJ,2) = ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,0)
         ISYMOFF(IRREPI,IRREPJ,3) = ISYMOFFSO(IRREPI,IRREPJ, POP, POP,0)
         ISYMOFF(IRREPI,IRREPJ,4) = ISYMOFFSO(IRREPI,IRREPJ, POP, POP,0)
         ISYMOFF(IRREPI,IRREPJ,5) = ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,2)
         ISYMOFF(IRREPI,IRREPJ,6) = ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,2)
         ISYMOFF(IRREPI,IRREPJ,7) = ISYMOFFSO(IRREPI,IRREPJ, POP, POP,2)
         ISYMOFF(IRREPI,IRREPJ,8) = ISYMOFFSO(IRREPI,IRREPJ, POP, POP,2)
         ISYMOFF(IRREPI,IRREPJ,9) = ISYMOFFSO(IRREPI,IRREPJ, VRT, POP,1)
         ISYMOFF(IRREPI,IRREPJ,10)= ISYMOFFSO(IRREPI,IRREPJ, VRT, POP,1)
         ISYMOFF(IRREPI,IRREPJ,11)= ISYMOFFSO(IRREPI,IRREPJ, VRT, POP,1)
         ISYMOFF(IRREPI,IRREPJ,12)= ISYMOFFSO(IRREPI,IRREPJ, VRT, POP,1)
         ISYMOFF(IRREPI,IRREPJ,13)= ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,14)= ISYMOFFSO(IRREPI,IRREPJ, POP, POP,1)
         ISYMOFF(IRREPI,IRREPJ,15)= ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,16)= ISYMOFFSO(IRREPI,IRREPJ, POP, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,17)= ISYMOFFSO(IRREPI,IRREPJ, POP, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,18)= ISYMOFFSO(IRREPI,IRREPJ, POP, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,19)= ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,20)= ISYMOFFSO(IRREPI,IRREPJ, VRT, VRT,1)
         ISYMOFF(IRREPI,IRREPJ,21)= ISYMOFFSO(IRREPI,IRREPJ, POP, POP,1)
         ISYMOFF(IRREPI,IRREPJ,22)= ISYMOFFSO(IRREPI,IRREPJ, POP, POP,1)
      ENDDO
      ENDDO 
      write(6,*)'ISYMOFF', ISYMOFF(1,1,9), ISYMOFF(2,2,9)
      DO IRREP = 1, NIRREP 
         IAOPOP(IRREP) = POPA(IRREP) + VRTA(IRREP)
      ENDDO 
      NOCCO(1)=NOA; NOCCO(2) = NOA; NVRTO(1) = NVA; NVRTO(2) = NVA
      write(6,*)'nirrep in bdf', nirrep 
      write(6,*)'symx,symry,symrz in bdf', symrx, symry, symrz
      OPEN(UNIT=1000, FILE='bdfcommon', FORM='UNFORMATTED')
      REWIND 1000
      WRITE(1000) NOCCO(1), NOCCO(2), NVRTO(1), NVRTO(2)     ! USED
      WRITE(1000) (((ISYMOFF(I,J,K),I=1,8),J=1,8),K=1,25)    ! USED
      WRITE(1000) ((POP(I,J),I=1,8),J=1,2)                   ! USED    
      WRITE(1000) ((VRT(I,J),I=1,8),J=1,2)                   ! USED 
      WRITE(1000) NT(1), NT(2), NF1(1),NF1(2),NF2(1),NF2(2)  ! USED
      WRITE(1000) SYMRX, SYMRY, SYMRZ                    ! USED
      WRITE(1000) ISYMV                                  ! USED
      WRITE(1000) (((IOFFT2(I,J,K),I=1,8),J=1,8),K=1,7)  ! USED
      WRITE(1000) (((IOFFAKIJ(I,J,K),I=1,8),J=1,8),K=1,4)! USED IN T
      WRITE(1000) (((IOFFABC(I,J,K),I=1,8),J=1,8),K=1,8) ! USED IN T
      WRITE(1000) (((IOFFIJK(I,J,K),I=1,8),J=1,8),K=1,8) ! USED IN T
      WRITE(1000) (((IOFFABI(I,J,K),I=1,8),J=1,8),K=1,8) ! USED IN T
      WRITE(1000) ((NUMABC(I,J),I=1,8),J=1,8)    ! USED IN T
      WRITE(1000) ((NUMIJK(I,J),I=1,8),J=1,8)    ! USED IN T   
      WRITE(1000) ((NUMABI(I,J),I=1,8),J=1,8)    ! USED IN T 
      WRITE(1000) (NMAXABC(I),I=1,8)             ! USED IN T
      WRITE(1000) (NMAXIJK(I),I=1,8)             ! USED IN T 
      WRITE(1000) (NMAXABI(I),I=1,8)             ! USED IN T
      WRITE(1000) ((IOFFSETW(I,J),I=1,8),J=1,8)  ! USED IN T
      WRITE(1000) ((IRREPA(I,J),I=1,255),J=1,2)  ! NOT USED
      WRITE(1000) ((DIRPRD(I,J),I=1,8),J=1,8)    ! USED
      WRITE(1000) (IAOPOP(I),I=1,8)              ! USED
      WRITE(1000) ((IRPDPD(I,J),I=1,8),J=1,22)   ! USED
      WRITE(1000) ((ISYTYP(I,J),i=1,2),J=1,500)  ! USED
      WRITE(1000) (NTOT(I),I=1,18)               ! NOT USED
      WRITE(1000) NIRREP, IINTFP                 ! USED
      CLOSE(1000) 
      write(6,*)'no nv', nocco(1),nvrto(1)
      write(6,*)'iintfp', iintfp 
CWZ==
      OPEN(UNIT=1001, FILE='bdfintegrals.data', FORM='UNFORMATTED')
      REWIND 1001 
      WRITE(1001) NIRREP, NOA, NVA
      WRITE(1001) ESCF
      WRITE(1001) (EA(I), I=1, NOA+NVA)
C       
      LSTI(1) = 7     !IJKA
      LSTI(2) = 9     !IjAk
      LSTI(3) = 10    !IjKa
      LSTI(4) = 11    !IJKL
      LSTI(5) = 13    !IjKl
      LSTI(6) = 14    !ABIJ
      LSTI(7) = 16    !AbIj
      LSTI(8) = 17    !<Ab|Ij>  as bjAI  
      LSTI(9) = 18    !<Ab|Ij>  as AIbj  
      LSTI(10) = 19   !<AB||IJ> as AIBJ  
      LSTI(11) = 21   !<Ab|Ij>  as AjbI
      LSTI(12) = 23   !<IA||JB> as BIAJ
      LSTI(13) = 25   !<iA|jB>  as BiAj
      LSTI(14) = 26   !<aI|bJ>  as bIaJ
      LSTI(15) = 27   !<AB||CI>
      LSTI(16) = 29   !<Ab||Ic>
      LSTI(17) = 30   !<Ab||Ci>
      LSTI(18) = 231  !<AB||CD>
      LSTI(19) = 233  !<Ab||Cd>
      fname(1)  = 'IJKAAA__'
      fname(2)  = 'IJAKAB__'
      fname(3)  = 'IJKAAB__'
      fname(4)  = 'IJKLAA__'
      fname(5)  = 'IJKLAB__'
      fname(6)  = 'ABIJAA__'
      fname(7)  = 'ABIJAB__'
      fname(8)  = 'BIAJAABB'
      fname(9)  = 'BIAJAABB'
      fname(10) = 'ABIJAA2_'
      fname(11) = 'ABIJAB2_'
      fname(12) = 'BIAJAAAA'
      fname(13) = 'BIAJABAB'
      fname(14) = 'BIAJABAB'
      fname(15) = 'ABCIAA__'
      fname(16) = 'ABICAB__'
      fname(17) = 'ABCIAB__'
      fname(18) = 'ABCDAA__'
      fname(19) = 'ABCDAB__'
C
      DO IL = 1, 19
         LST = LSTI(IL)
         NSIZE = IDSYMSZ(1, ISYTYP(1,LST), ISYTYP(2,LST)) 
         IF(NSIZE*2>MAXDIM) THEN
            WRITE(6,*) 'MEMORY TOO SMALL!', NSIZE, MAXDIM
!           STOP
         ENDIF
!         CALL GETALLSO(TMPMEM, 1, fname(IL))
         IF(LST==19) THEN
             i0 = 1; i1 = i0+NSIZE; i2 = i1 + nsize
!            call iexpso(tmpmem, tmpmem(i1), vrta, vrta, 0, 1,
!    &                           popa, popa, 0, 0, 1)
!            call iexpso(tmpmem(i1), tmpmem(i2), vrta, vrta, 1, 0,
!    &                           popa, popa, 0, 1, 1)
!            call sstgenso(tmpmem(i2), tmpmem, nsize, vrta, vrta, popa,
!    &                           popa, tmpmem, 1, '1324')
         ELSEIF(LST==21) THEN
!            i0 = 1; i1 = i0 + nsize 
!            call sstgenso(tmpmem, tmpmem(i1), nsize, vrta, vrta, popa,
!    &                           popa, tmpmem, 1, '1423')
!            call myicopyso(tmpmem(i1), tmpmem, nsize) 
         ELSEIF(LST==23.or.LST==25.or.LST==26) THEN 
!            call scalso(nsize, -1.d0, tmpmem, 1) 
         ENDIF    
!        WRITE(1001) LST, NSIZE
!        WRITE(1001) (TMPMEM(I), I = 1, NSIZE)
      ENDDO
      CLOSE(1001)
C
!     OPEN(UNIT=1002, FILE='bdffock.data', FORM='UNFORMATTED')
!     REWIND 1002 
!     WRITE(1002) ((FOCKIJA(I,J), I = 1, NOA), J = 1, NOA)
!     WRITE(1002) ((FOCKIJAAI(I,J), I = 1, NOA), J = 1, NOA)
!     WRITE(1002) ((FOCKIJABR(I,J), I = 1, NOA), J = 1, NOA)
!     WRITE(1002) ((FOCKIJABI(I,J), I = 1, NOA), J = 1, NOA)
!     WRITE(1002) ((FOCKABA(I,J), I = 1, NVA), J = 1, NVA)
!     WRITE(1002) ((FOCKABAAI(I,J), I = 1, NVA), J = 1, NVA)
!     WRITE(1002) ((FOCKABABR(I,J), I = 1, NVA), J = 1, NVA)
!     WRITE(1002) ((FOCKABABI(I,J), I = 1, NVA), J = 1, NVA)
!     WRITE(1002) ((FOCKIAA(I,J), I = 1, NVA), J = 1, NOA)
!     WRITE(1002) ((FOCKIAAAI(I,J), I = 1, NVA), J = 1, NOA)
!     WRITE(1002) ((FOCKIAABR(I,J), I = 1, NVA), J = 1, NOA)
!     WRITE(1002) ((FOCKIAABI(I,J), I = 1, NVA), J = 1, NOA)
!     CLOSE(1002)
!     CALL WRITEZMAT

      RETURN
      END
